<template>
  <st-table-panel>
    <div slot="btn" style="text-align: right;">
      <el-button
        type="primary"
        plain
        icon="el-icon-plus"
        @click="handleAdd"
        v-hasPermi="['crm:contacts:add']"
      >新增</el-button>
    </div>
    <div slot="content">
      <el-table v-loading="loading" :height="tableHeight" border :data="contactsList" @selection-change="handleSelectionChange">
        <div slot="empty">
          <el-empty :image="noData" image-size="200">
          </el-empty>
        </div>
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="姓名" align="center" prop="contactsName" />
        <el-table-column label="手机" align="center" prop="mobile" />
        <el-table-column label="电话" align="center" prop="telephone" />
        <el-table-column label="职务" align="center" prop="duties" />
        <el-table-column label="详细地址" align="center" prop="address" />
        <el-table-column label="下次联系时间" align="center" prop="nextContactTime" />
        <el-table-column label="客户" align="center" prop="customerId" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['crm:contacts:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['crm:contacts:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 添加或修改联系人对话框 -->
      <st-dialog :title="title" v-if="open" @close="cancel" padding="100px 0 150px"
                 @save="submitForm"  append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="180px">
          <el-form-item label="姓名：" prop="contactsName">
            <el-input v-model="form.contactsName" placeholder="请输入姓名" />
          </el-form-item>
          <el-form-item label="手机：" prop="mobile">
            <el-input v-model="form.mobile" placeholder="请输入手机" />
          </el-form-item>
          <el-form-item label="电话：" prop="telephone">
            <el-input v-model="form.telephone" placeholder="请输入电话" />
          </el-form-item>
          <el-form-item label="部门：" prop="department">
            <el-input v-model="form.department" placeholder="请输入部门" />
          </el-form-item>
          <el-form-item label="职务：" prop="duties">
            <el-input v-model="form.duties" placeholder="请输入职务" />
          </el-form-item>
          <el-form-item label="生日：" prop="birthday">
            <el-input v-model="form.birthday" placeholder="请输入生日" />
          </el-form-item>
          <el-form-item label="邮箱：" prop="mailbox">
            <el-input v-model="form.mailbox" placeholder="请输入邮箱" />
          </el-form-item>
          <el-form-item label="微信号：" prop="weChat">
            <el-input v-model="form.weChat" placeholder="请输入微信号" />
          </el-form-item>
          <el-form-item label="QQ号：" prop="qq">
            <el-input v-model="form.qq" placeholder="请输入QQ号" />
          </el-form-item>
          <el-form-item label="详细地址：" prop="address">
            <el-input v-model="form.address" placeholder="请输入详细地址" />
          </el-form-item>
          <el-form-item label="亲密度：" prop="intimacy">
            <el-input v-model="form.intimacy" placeholder="请输入亲密度" />
          </el-form-item>
          <el-form-item label="重要程度：" prop="important">
            <el-input v-model="form.important" placeholder="请输入重要程度" />
          </el-form-item>
          <el-form-item label="下次联系时间：" prop="nextContactTime">
            <el-input v-model="form.nextContactTime" placeholder="请输入下次联系时间" />
          </el-form-item>
          <el-form-item label="是否为默认联系人" prop="dc">
            <el-input v-model="form.dc" placeholder="请输入是否为默认联系人" />
          </el-form-item>
          <el-form-item label="是否为关键决策人" prop="dm">
            <el-input v-model="form.dm" placeholder="请输入是否为关键决策人" />
          </el-form-item>
          <el-form-item label="客户" prop="customerId">
            <el-input v-model="form.customerId" placeholder="请输入客户" />
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" placeholder="请输入备注" />
          </el-form-item>
          <el-form-item label="负责人" prop="ownerUserId">
            <el-input v-model="form.ownerUserId" placeholder="请输入负责人" />
          </el-form-item>
        </el-form>
      </st-dialog>
    </div>
    <div slot="footer">
      <pagination
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
  </st-table-panel>
</template>

<script>
import { listContacts, getContacts, delContacts, addContacts, updateContacts } from "@/api/crm/contacts";
import noData from '@/assets/img/no_data.png'
export default {
  name: "Contacts",
  data() {
    return {
      noData:noData,
      tableHeight:undefined,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 联系人表格数据
      contactsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        contactsName: null,
        mobile: null,
        telephone: null,
        department: null,
        duties: null,
        sex: null,
        birthday: null,
        mailbox: null,
        weChat: null,
        qq: null,
        address: null,
        intimacy: null,
        important: null,
        nextContactTime: null,
        dc: null,
        dm: null,
        customerId: null,
        deltag: null,
        ownerUserId: null,
        createdBy: null,
        createdTime: null,
        updatedBy: null,
        updatedTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询联系人列表 */
    getList() {
      this.loading = true;
      listContacts(this.queryParams).then(response => {
        this.contactsList = response.rows;
        this.total = response.total;
        this.loading = false;
        this.$nextTick(()=>{
          this.tableHeight=this.getTableHeight() -47;
        })
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        contactsId: null,
        contactsName: null,
        mobile: null,
        telephone: null,
        department: null,
        duties: null,
        sex: null,
        birthday: null,
        mailbox: null,
        weChat: null,
        qq: null,
        address: null,
        intimacy: null,
        important: null,
        nextContactTime: null,
        dc: null,
        dm: null,
        customerId: null,
        deltag: null,
        remark: null,
        ownerUserId: null,
        createdBy: null,
        createdTime: null,
        updatedBy: null,
        updatedTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.contactsId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加联系人";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const contactsId = row.contactsId || this.ids
      getContacts(contactsId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改联系人";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.contactsId != null) {
            updateContacts(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addContacts(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const contactsIds = row.contactsId || this.ids;
      this.$modal.confirm('是否确认删除联系人编号为"' + contactsIds + '"的数据项？').then(function() {
        return delContacts(contactsIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('crm/contacts/export', {
        ...this.queryParams
      }, `contacts_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped lang="scss">
.page-container{
  height: calc(100vh - 269px) ;
}
</style>
